import logging
import sys
import os
import pytest

from config.config import *

# __name__ 是一个内置变量，当文件被运行时，__name__ 被设置为 __main__
# 当被导入到其他文件时，则判断条件不成立

# --alluredir  ./report/test   指定一个目录，并生成中间结果
# --clean-alluredir 每次运行会清空中间结果

# os.system(命令)   相当于在cmd中执行命令
# allure generate  中间结果目录 -o 目标html报告目录 --clean
# run.py 顶部添加

if __name__ == "__main__":   #用于运行环境检测
    if len(sys.argv) == 2:
        if sys.argv[1] == "test":
            #测试环境的地址
            os.environ["URL"] = "http://192.168.10.141/api/cloud-api"
            os.environ["DB"] = str({
                "host": "192.168.10.141",
                "port": 3306,
                "database": "xingyun",
                "user": "root",
                "password": "123456"
            })
            os.environ["ENV"] = "test"
        elif sys.argv[1] == "pord":
            # BASE_URL = "http://192.168.10.141/api/cloud-api"
            # DB_HOST = "192.168.10.141"
            # DB_PORT = 3306
            # DB_NAME = "xingyun"
            # DB_USER = "root"
            # DB_PASSWORD = "123456"
            # DB_CHARSET = "utf8"
            os.environ["URL"] = "http://192.168.10.141/api/cloud-api"
            os.environ["DB"] = str({
                "host":"192.168.10.141",
                "port":3306,
                "database":"xingyun",
                "user":"root",
                "password":"123456"
            })
            os.environ["ENV"] = "pord"
        else:
            logging.info("请传入正确的执行命令：python run.py test 或者 python run.py pord")
    else:
        logging.info("请传入正确的执行命令：python run.py test 或者 python run.py pord")

    pytest.main(["-vs","./test_scripts/test_runner.py","--alluredir",".report/json_report","--clean-alluredir"])
    os.system("allure generate .report/json_report -o .report/html_report --clean")

    #发送测试报告
    from utils.send_feishu_msg_utils import send_feishu_message
    send_feishu_message(WEBHOOK)



